﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// Provides sorting of nodes in a TreePanel 
    /// </summary>    
    public class ExtTreeSorter : ExtComponent
    {
        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.tree.TreeNode", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.tree.TreeSorter";
            desc.Overloaded = true;

            String[] properties = new String[]{
                "caseSensitive", "dir", "folderSort", "leafAttr", "property", "sortType"       
            };

            Type component = typeof(ExtTreeSorter);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtTreeSorter.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            if (!String.IsNullOrEmpty(targetID))
            {
                if (String.Compare(targetID, GetControlID(targetID), true) == 0)
                    desc.AddPropertyBase("targetID", String.Format("Ext.getCmp('{0}')", targetID));
                else
                    desc.AddPropertyBase("targetID", String.Format("{0}", GetControlID(targetID)));
            }

            return list;
        }

        #region Public Properties

        /// <summary>
        /// tree ID to sort
        /// </summary>
        [Description("tree panel ID to sort.")]
        public String targetID { get; set; }

        /// <summary>
        /// true for case sensitive sort (defaults to false)
        /// </summary>
        [Description("true for case sensitive sort (defaults to false)")]
        public Boolean? caseSensitive { get; set; }

        /// <summary>
        /// The direction to sort (asc or desc) (defaults to asc)
        /// </summary>
        [Description("The direction to sort (asc or desc) (defaults to asc)")]
        public String dir { get; set; }
        
        /// <summary>
        /// True to sort leaf nodes under non leaf nodes
        /// </summary>
        [Description("True to sort leaf nodes under non leaf nodes")]
        public Boolean? folderSort { get; set; }

        /// <summary>
        /// The attribute used to determine leaf nodes in folder sort (defaults to "leaf")
        /// </summary>
        [Description("The attribute used to determine leaf nodes in folder sort (defaults to leaf)")]
        public String leafAttr { get; set; }

        /// <summary>
        /// The named attribute on the node to sort by (defaults to text)
        /// </summary>
        [Description("The named attribute on the node to sort by (defaults to text)")]
        public String property { get; set; }

        /// <summary>
        /// A custom "casting" function used to convert node values before sorting
        /// </summary>
        [Description("A custom casting function used to convert node values before sorting")]
        public String sortType { get; set; }


        #endregion
    }
}
